package com.gmrz.uap.serviceplugin;

import com.gmrz.uap.cache.AmmsCacheUtil;
import com.gmrz.uap.cache.CacheStoreService;
import com.gmrz.uap.cfg.SystemConst;
import com.gmrz.uap.common.DbConst;
import com.gmrz.uap.common.ServiceSupport;
import com.gmrz.uap.model.PluginArgument;
import com.gmrz.uap.model.ServicePlugin;
import com.gmrz.uap.util.Identities;
import com.jfinal.aop.Before;
import com.jfinal.plugin.activerecord.Page;
import com.jfinal.plugin.activerecord.tx.Tx;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import redis.clients.jedis.exceptions.JedisConnectionException;


public class ServicePluginService extends ServiceSupport {
    private static final Logger LOG = LogManager.getLogger(ServicePluginService.class);

    public static final ServicePluginService me = new ServicePluginService();

    /**
     * 根据页面传过来的条件进行分页查询
     *
     * @param pluginArgument 插件参数信息
     * @param page           页号
     * @param rows           行数
     * @param sort           排序字段
     * @param order          升序/降序
     * @return　分页结果 Page<PluginArgument>
     */
    public Page<PluginArgument> findListForPaginate(PluginArgument pluginArgument, int page, int rows, String sort, String order, String tenantid) {
        return PluginArgument.dao.findListForPaginate(pluginArgument, page, rows, sort, order, tenantid);
    }

    /**
     * 增加插件参数信息
     *
     * @param pluginArgument 插件参数信息
     * @return true/false
     */
    public boolean save(PluginArgument pluginArgument) {
        pluginArgument.set(DbConst.TABLE_PLUGIN_ARGUMENT_COLUMN_PA_ID, Identities.uuid2());
        return pluginArgument.save();
    }

    /**
     * 修改插件参数信息
     *
     * @param pluginArgument 插件参数信息
     * @return true/false
     */
    public boolean update(PluginArgument pluginArgument) {
        return pluginArgument.update();
    }

    /**
     * 根据参数删除插件参数信息，ID可以是多个，中间使用逗号隔开
     *
     * @param ids ID集合
     * @return true/false
     */
    @Before(Tx.class)
    public boolean remove(String ids) {
        String[] idArr = ids.split(SystemConst._DEFAULT_SEPARATOR_COMMA);

        for (String id : idArr) {
            flag = PluginArgument.dao.deleteById(id);
        }
        return flag;
    }

    public boolean exist(String configName) {
        PluginArgument pluginArgument = PluginArgument.dao.findByName(configName);
        return null == pluginArgument;
    }

    /**
     * 更新缓存
     *
     * @param spId
     */
    public String modifyCache(String spId, String message) throws JedisConnectionException {
        //CacheStoreService cacheStoreService = AmmsCacheUtil.getCacheStoreService();
        ServicePlugin servicePlugin = ServicePlugin.dao.findById(spId);
        String cacheKey = String.format(AmmsCacheUtil.CACHE_SERVICE_PLUGIN, servicePlugin.getStr(DbConst.TABLE_SERVICE_PLUGIN_COLUMN_AAID));
        //cacheStoreService.remove(cacheKey);
        return AmmsCacheUtil.deleteIfSuccess(cacheKey, message);

    }

}
